package cn.t.service.device;

import cn.t.constants.CommonConstants;
import cn.t.core.service.ServiceImpl;
import cn.t.model.android.DeviceAndroidConfigDO;
import cn.t.model.device.DeviceConfigRowDO;
import cn.t.repository.android.DeviceAndroidConfigRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 安卓功能配置表(DeviceAndroidConfig)领域服务
 *
 * @author t
 * @since 2024-05-24 10:19:11
 */
@Slf4j
@Service
@AllArgsConstructor
public class DeviceAndroidConfigService extends ServiceImpl<DeviceAndroidConfigDO, DeviceAndroidConfigRepository> {

    private final DeviceConfigRowService deviceConfigRowService;

    public DeviceAndroidConfigDO getConfig(String key, String value) {
        DeviceAndroidConfigDO queryParams = new DeviceAndroidConfigDO();
        queryParams.setKey(key);
        queryParams.setValue(value);
        List<DeviceAndroidConfigDO> configs = this.findByModel(queryParams, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
        return CollectionUtils.isEmpty(configs) ? null : configs.get(0);
    }

    public List<DeviceAndroidConfigDO> getAllConfigs(Long deviceId) {

        // 根据设备id查找配置id
        DeviceConfigRowDO configRowQueryParam = new DeviceConfigRowDO();
        configRowQueryParam.setDeviceId(deviceId);
        List<DeviceConfigRowDO> deviceConfigRows = this.deviceConfigRowService.findByModel(configRowQueryParam, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
        if (CollectionUtils.isEmpty(deviceConfigRows)) {
            return Collections.emptyList();
        }
        List<Long> configIds = deviceConfigRows.stream().map(DeviceConfigRowDO::getConfigId).collect(Collectors.toList());

        DeviceAndroidConfigDO queryParam = new DeviceAndroidConfigDO();
        queryParam.setConfigIds(configIds);
        queryParam.setReleased(true);
        return this.findByModel(queryParam, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
    }

    public DeviceConfigRowDO buildConfigRow(Long configId, Long deviceId) {
        DeviceConfigRowDO configRowDO = new DeviceConfigRowDO();
        configRowDO.setConfigId(configId);
        configRowDO.setDeviceId(deviceId);
        return configRowDO;
    }

    /**
     * 新增设备默认绑定所有全量发布的配置
     * @param deviceId 主设备id
     */
    public void bindFullPublishConfigs(Long deviceId) {
        // 查询全量发布配置
        DeviceAndroidConfigDO queryParam = new DeviceAndroidConfigDO();
        queryParam.setReleaseType(CommonConstants.ONE);
        List<DeviceAndroidConfigDO> deviceAndroidConfigDOS = this.findByModel(queryParam, CommonConstants.ZERO, Integer.MAX_VALUE, null, null);
        // 新设备绑定全量发布配置
        List<DeviceConfigRowDO> configRows = deviceAndroidConfigDOS.stream().map(config -> this.buildConfigRow(config.getId(), deviceId)).collect(Collectors.toList());
        this.deviceConfigRowService.addBatch(configRows);
    }
}
